/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatsoft.client.ui;

import ChatSoft.model.UserData;
import ChatSoft.model.User;
import chatsoft.client.buz.UserBussiness;
import java.io.ObjectOutputStream;
import javax.swing.JOptionPane;

/**
 *
 * @author KID
 */
public class AddFriendFrame extends javax.swing.JFrame {

    /**
     * Creates new form AddFriendFrame
     */
    ObjectOutputStream outStream;
    
    User user;
    UserBussiness userHandle;

    public AddFriendFrame(ObjectOutputStream outStream, User user) {
        initComponents();
        this.outStream = outStream;
  
        this.user = user;
        userHandle = new UserBussiness(this.outStream);
        setLocationRelativeTo(this);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtFriendName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnAddFriend = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Friend's UserName:");

        btnAddFriend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/chatsoft/client/image/add_user.png"))); // NOI18N
        btnAddFriend.setText("add Friend");
        btnAddFriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddFriendActionPerformed(evt);
            }
        });

        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/chatsoft/client/image/delete.png"))); // NOI18N
        btnExit.setText("Exit");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtFriendName, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(btnAddFriend)
                        .addGap(18, 18, 18)
                        .addComponent(btnExit)))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFriendName, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAddFriend, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(29, 29, 29))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    UserData dataTranfer;
    private void btnAddFriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddFriendActionPerformed
        if (txtFriendName.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Please enter your friend's name!");
            return;
        }
        user.getFriendsList().add(txtFriendName.getText());
        userHandle.updateUser(user);
        this.dispose();

    }//GEN-LAST:event_btnAddFriendActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddFriend;
    private javax.swing.JButton btnExit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txtFriendName;
    // End of variables declaration//GEN-END:variables
}
